home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Papers / C++ Exceptions / µShell / Core Utilities / SmartRef.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-25  |  2.7 KB  |  163 lines  |  [TEXT/CWIE]

  1. #ifndef __OBJECTREF__
  2. #define __OBJECTREF__
  3. #pragma once
  4.  
  5. #include "ObjectPtr.h"
  6.  
  7. //----------------------------------------------------------------------------
  8. // ObjectReference - a reference to a reference counted object.
  9. //----------------------------------------------------------------------------
  10.  
  11. template<class T>
  12. class SmartRef
  13. {
  14. public:
  15.     T&    ref;
  16.  
  17.     SmartRef(T* obj) : ref(NewObjectRef(*obj))
  18.     { 
  19.         if (ptr == obj)
  20.             TObject::NilObjectReference();
  21.         else
  22.             TObject::NewObjectRef(obj);
  23.     }
  24.     inline SmartRef(T* obj)                  : ref(obj)          { NewObjectRef(*obj); }
  25.     inline SmartRef(T& obj)                  : ref(obj)          { NewObjectRef(obj); }
  26.     inline SmartRef(const SmartRef<T>& ref) : ref(ref.ref)    { NewObjectRef(ref); }
  27.  
  28. //    SmartRef(const T& obj) const    : ref(&obj)                 { NewObjectRef(const_cast<TObject*>(&obj)); }
  29. //    SmartRef(T& obj)                : ref(&obj)                 { NewObjectRef(&obj); }
  30. //    SmartRef(const T* obj) const    : ref(const_cast<T*>(obj))    { NewObjectRef(const_cast<T*>(obj));  }
  31.  
  32. #if    MEMBER_TEMPLATES_SUPPORTED
  33.  
  34.     template<class U>
  35.     inline SmartRef(SmartRef<U>& rhs) : ref(&rhs)              { rhs.NewReference(); }
  36.  
  37. #else
  38.  
  39. //    SmartRef(TObject* rhs) : ref(dynamic_cast<T*>(rhs))    { NewObjectRef(ref); }
  40.  
  41. #endif
  42.  
  43.     ~SmartRef()
  44.     {
  45.         TObject::DeleteObjectRef(ref);
  46.     }
  47.     
  48.     operator T&()    { return ref; }
  49.  
  50. #if MEMBER_TEMPLATES_SUPPORTED
  51.  
  52.     inline SmartRef<T>& operator=(const SmartRef<U&> rhs)
  53.     {
  54.         if (this != &rhs)
  55.         {
  56.             ref = SwapObjectRef(ref, rhs);
  57.         }
  58.         
  59.         return *this
  60.     }
  61.  
  62. #else
  63.  
  64.     inline SmartRef<T>& operator=(const SmartRef<T>& rhs)
  65.     {
  66.         if (ref != rhs.ref)
  67.         {
  68.             TObject::SwapObjectRef(ref, rhs);
  69.             ref = rhs.ref;
  70.         }
  71.         
  72.         return *this;
  73.     }
  74.  
  75.     inline SmartRef<T>& operator=(T& rhs)
  76.     {
  77.         if (ref != rhs)
  78.         {
  79.             TObject::SwapObjectRef(ref, rhs);
  80.             ref = rhs;
  81.         }
  82.         
  83.         return *this;
  84.     }
  85.  
  86. /*
  87.     SmartRef<T>& operator=(TObject* param)
  88.     {
  89.         T* obj = dynamic_cast<T*>(param);
  90.  
  91.         if (ref != obj)
  92.         {
  93.             SwapObjectRef(ref, obj);
  94.             ref = obj;
  95.         }
  96.         
  97.         return *this;
  98.     }
  99. */
  100.  
  101. #endif
  102.  
  103.     inline bool operator==(const T* obj) const
  104.     {
  105.         return ref == obj;
  106.     }
  107.  
  108.     inline bool operator==(const T& obj) const
  109.     {
  110.         return ref == &obj;
  111.     }
  112.  
  113.     inline bool operator==(SmartRef<T>& obj) const
  114.     {
  115.         return ref == obj.ref;
  116.     }
  117.  
  118.     inline bool operator!=(const T& obj) const
  119.     {
  120.         return ref != &obj;
  121.     }
  122.  
  123.     inline bool operator!=(const SmartRef<T>& obj) const
  124.     {
  125.         return ref != obj.ref;
  126.     }
  127.  
  128.     inline bool operator!=(ObjectPtr<T>& obj) const
  129.     {
  130.         return ref != obj.ref;
  131.     }
  132.  
  133.     inline const T* operator&() const
  134.     {
  135.         return &ref;
  136.     }
  137.  
  138.     inline T* operator&()
  139.     {
  140.         return &ref;
  141.     }
  142. };
  143.  
  144. /*
  145. template<class T, class U>
  146.     SmartRef<T>& operator=(SmartRef<U&> rhs)
  147.     {
  148.         if (this != &rhs)
  149.         {
  150.             ref = SwapObjectRef(ref, rhs);
  151.         }
  152.         
  153.         return *this
  154.     }
  155. */
  156.  
  157. typedef SmartRef<const TObject>    ConstObjectRef;
  158. //typedef SmartRef<TObject>             SmartRef;
  159.  
  160.  
  161. #endif __OBJECTREF__
  162.  
  163.